<template>
  <div class="w-[1150px] print-style relative bg-white p-6 h-full">
    <div class="flex justify-between w-full text-center items-center pl-60">
      <div class="flex-1 text-center text-[32px]">{{ getSystemConfig('HISNAME') }}</div>
      <BaseBarcode :value="tableData[0]?.lastWardId" class="w-60 text-right" />
    </div>
    <div class="w-full flex text-[22px] justify-between">
      <div class="w-1/4">{{ tableData[0]?.lastWardName }}</div>
      <div class="text-center flex-1">住院病房发药单（{{ printType === '0' ? '针剂汇总' : '针剂' }} ）</div>
      <div class="w-1/4"></div>
    </div>
    <div class="mt-4 text-lg">
      <!-- 1:针剂  -->
      <template v-if="printType === '1'">
        <div v-for="(item, index) in tableData" :key="index" class="print-table">
          <div class="flex w-full overflow-hidden">
            <div class="print-table-item w-[60px]">床位</div>
            <div class="print-table-item w-[100px]">姓名</div>
            <div class="print-table-item flex-1">药品</div>
            <div class="print-table-item w-[60px] text-right">数量</div>
            <div class="print-table-item w-[60px]">单位</div>
            <div class="print-table-item w-[160px]">申请时间</div>
            <div class="print-table-item w-[100px]">下单医生</div>
          </div>
          <div v-for="(itemRow, indexRow) in item.list" :key="indexRow" class="flex w-full overflow-hidden">
            <div class="print-table-item w-[60px]">{{ itemRow.lastBedCode }}</div>
            <div class="print-table-item w-[100px]">{{ itemRow.brName }}</div>
            <div class="print-table-item flex-1">{{ itemRow.advNameNoCj }}</div>
            <div class="print-table-item w-[60px] text-right">{{ itemRow.minimumCount }}</div>
            <div class="print-table-item w-[60px]">{{ itemRow.minimumUnit }}</div>
            <div class="print-table-item w-[160px]">{{ dayjs(itemRow.dysj).format('MM-DD HH:mm:ss') }}</div>
            <div class="print-table-item w-[100px]">{{ itemRow.orderDoctor }}</div>
          </div>
          <div class="flex w-full overflow-hidden">
            <div class="print-table-item w-[60px]"></div>
            <div class="print-table-item w-[100px]">小计</div>
            <div class="print-table-item flex-1">{{ item.list[0]?.advName }}</div>
            <div class="print-table-item w-[60px] text-right">{{ item.list.reduce((a, b) => a + b.minimumCount, 0) }}</div>
            <div class="print-table-item w-[60px]">{{ item.list[0]?.minimumUnit }}</div>
            <div class="print-table-item w-[160px]"></div>
            <div class="print-table-item w-[100px]"></div>
          </div>
        </div>
      </template>
      <!-- 2：汇总 -->
      <template v-else-if="printType === '0'">
        <div class="print-table">
          <div class="flex w-full">
            <div class="print-table-item w-[60px]">序号</div>
            <div class="print-table-item flex-1">药品</div>
            <div class="print-table-item w-[80px] text-right">数量</div>
            <div class="print-table-item w-[100px]">单位</div>
          </div>
          <div v-for="(item, index) in tableData" :key="index" class="flex w-full">
            <div class="print-table-item w-[60px]">{{ index + 1 }}</div>
            <div class="print-table-item flex-1">{{ item.advName }}</div>
            <div class="print-table-item w-[80px] text-right">{{ item.minimumCount }}</div>
            <div class="print-table-item w-[100px]">{{ item.minimumUnit }}</div>
          </div>
        </div>
      </template>
    </div>
    <div class="flex justify-between mt-6">
      <div class="flex">
        接收护士：
        <div class="border-bottom h-6 w-20"></div>
      </div>
      <div>发药人：{{ empName }}</div>
      <div>打印时间：{{ dayjs().format('YYYY-MM-DD HH:mm:ss') }}</div>
    </div>
  </div>
</template>

<script setup>
import dayjs from 'dayjs'
import { getSystemConfig } from '@/utils/systemConfig'
import useUserStore from '@/store/user'

const { empName } = useUserStore()
const props = defineProps({
  printData: {
    type: Object,
    default: () => ({})
  },
  printType: {
    type: String,
    default: '0'
  }
})

const tableData = ref([])

watch(
  () => props.printData,
  (val) => {
    // 使用 reduce 方法来分组对象
    tableData.value = val.printData.detail.reduce((acc, item) => {
      const advCode = item.advCode

      // 检查累加器中是否已经有这个barcode的条目
      if (!acc.some((group) => group.advCode === advCode)) {
        // 如果没有，则创建一个新的条目
        acc.push({
          ...item,
          list: []
        })
      }
      // 找到对应的barcode条目，并向其list中添加yzmc和rq
      const group = acc.find((g) => g.advCode === advCode)
      group.list.push({ ...item })
      return acc
    }, [])
  }
)
</script>
<style lang="less" scoped>
.print-table + .print-table {
  border-top: none;
}
.border-bottom {
  border-bottom: 1px solid #000000;
}
</style>
